/**
 * Created by Edwin on 2015/12/16 0016.
 */

var url = require('url');
//加密类
var crypto = require("crypto");
var mongoose = require('mongoose');
var shortid = require('shortid');
var Ingredient=require('../models/Ingredient');


//站点配置
var settings = require("../models/db/settings");
var db =null;
    //mongoose.connect('mongodb://'+settings.URL+":"+settings.PORT+"/"+settings.DB);

var DbOpt = {
    del : function(obj,targetId,fun){//删除操作

        if(shortid.isValid(targetId)){
            obj.remove({_id : targetId},function(err,result){
                if(err){
                    console.log(err);
                }else{
                    fun(result);

                }
            })
        }else{
            console.log(settings.system_illegal_param);
        }

    },
    delByCondition:function(obj,conditions,fun){
        obj.remove(conditions,function(err,result){
            if(err){
                console.log(err);
            }else{
                fun(result);

            }
        })

    },
    findAll : function(obj,fun){//查找指定对象所有记录
        obj.find({}, function (err,result) {
            if(err){
                console.log(err);
            }else{
                console.log(result);
                fun(result);
            }
        })
    },
    findOne : function(obj,targetId,fun){ //根据ID查找单条记录

        if(shortid.isValid(targetId)){
            obj.findOne({_id : targetId}, function (err,result) {
                if(err){
                    console.log(err);
                }else{
                    fun(result);
                }
            })
        }else{
            console.log(settings.system_illegal_param);
        }

    },
    updateOneByID : function(obj,targetId,addData,fun){
        if(shortid.isValid(targetId)){
            var conditions = {_id : targetId};
            var update = {$set : addData};
            obj.update(conditions, update, function (err,result) {
                if(err){
                    console.log(err);
                }else{
                    fun(result);
                }
            })
        }else{
            console.log(settings.system_illegal_param);
        }
    },
    update:function(obj,conditions,addData,fun){
        var update = {$set : addData};
        obj.update(conditions, update, function (err,result) {
            if(err){
                console.log(err);
            }else{
                fun(result);
            }
        });
    },
    addOne : function(obj,addData,fun){
        var newObj = new obj(addData);
        newObj.save(function(err){
            if(err){
                console.log(err);
            }else{
               fun();
            }
        });
    },

    pagination : function(obj,conditions,currentPage,limit,fun){

        var startNum = (currentPage - 1)*limit + 1;

        //根据条件查询记录(如果有条件传递，则按条件查询)
        var query;
        if(conditions && conditions.length > 1){
            query=obj.find().or(conditions);
        }
        else if(conditions) {
            query = obj.find(conditions);
        }else{
            query=obj.find({});
        }
        query.sort({'date': -1});

        if(obj === Ingredient){
            query.populate('type');
        }

        query.exec(function(err,docs){
            if(err){
                console.log(err);
            }else {
                return fun({
                    "adocs":docs,
                    "totalItems" : docs.length,
                    "currentPage" : currentPage,
                    "limit" : limit,
                    "startNum" : Number(startNum),
                    "docs" : docs.slice(startNum - 1,startNum + limit -1),
                    "totalPage":Math.ceil(docs.length/limit)
                });
            }
        })
    },

    getCount : function(obj,req,res,conditions){ // 查询指定对象的数量
        obj.count(conditions, function (err, count) {
            if (err){
                console.log(err);
            }else{
                return res.json({
                    count : count
                });
            }

        });
    },
    encrypt : function(data,key){ // 密码加密
        var cipher = crypto.createCipher("bf",key);
        var newPsd = "";
        newPsd += cipher.update(data,"utf8","hex");
        newPsd += cipher.final("hex");
        return newPsd;
    },
    decrypt : function(data,key){ //密码解密
        var decipher = crypto.createDecipher("bf",key);
        var oldPsd = "";
        oldPsd += decipher.update(data,"hex","utf8");
        oldPsd += decipher.final("utf8");
        return oldPsd;
    }
};



module.exports = DbOpt;